const DB_NAME = 'fames'
const DB = db.getSiblingDB(DB_NAME)
DB.section_conversations.find({ conversation_id: { $exists: false } }).forEach(doc => {
  DB.section_conversations.updateOne(
    { _id: doc._id },
    { $set: { conversation_id: new ObjectId().toString(), created_at: doc.created_at || new Date() } }
  )
})
DB.runCommand({
  collMod: 'section_conversations',
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['report_id', 'section_no', 'conversation_id'],
      properties: {
        report_id: { bsonType: 'string' },
        section_no: { bsonType: 'string' },
        conversation_id: { bsonType: 'string' },
        messages: {
          bsonType: 'array',
          items: {
            bsonType: 'object',
            properties: {
              role: { bsonType: 'string' },
              content: { bsonType: 'string' },
              created_at: { bsonType: 'date' },
              model_id: { bsonType: 'string' }
            },
            required: ['role', 'content']
          }
        },
        current_run_id: { bsonType: 'string' },
        status: { enum: ['active', 'closed'] },
        created_at: { bsonType: 'date' }
      }
    }
  },
  validationLevel: 'moderate',
  validationAction: 'error'
})
DB.section_conversations.createIndex({ report_id: 1, section_no: 1, conversation_id: 1 })
DB.runCommand({
  collMod: 'section_generation_runs',
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['report_id', 'section_no', 'input_snapshot', 'prompt', 'llm_params', 'metrics', 'status', 'created_at'],
      properties: {
        report_id: { bsonType: 'string' },
        section_no: { bsonType: 'string' },
        conversation_id: { bsonType: 'string' },
        input_snapshot: { bsonType: 'object' },
        prompt: { bsonType: 'string' },
        llm_params: {
          bsonType: 'object',
          properties: {
            provider_id: { bsonType: 'string' },
            model_id: { bsonType: 'string' },
            temperature: { bsonType: 'double' },
            top_p: { bsonType: 'double' },
            max_tokens: { bsonType: 'int' }
          }
        },
        messages: {
          bsonType: 'array',
          items: {
            bsonType: 'object',
            properties: {
              role: { bsonType: 'string' },
              content: { bsonType: 'string' },
              created_at: { bsonType: 'date' },
              model_id: { bsonType: 'string' }
            }
          }
        },
        output_text: { bsonType: 'string' },
        metrics: {
          bsonType: 'object',
          properties: {
            tokens_in: { bsonType: 'int' },
            tokens_out: { bsonType: 'int' },
            duration_ms: { bsonType: 'int' }
          }
        },
        parent_run_id: { bsonType: 'string' },
        selected_candidate_id: { bsonType: 'string' },
        status: { enum: ['success', 'failed'] },
        error: { bsonType: 'string' },
        created_at: { bsonType: 'date' },
        created_by: { bsonType: 'string' }
      }
    }
  },
  validationLevel: 'moderate',
  validationAction: 'error'
})
DB.section_generation_runs.createIndex({ report_id: 1, section_no: 1, conversation_id: 1, created_at: -1 })